package com.zff.rental.controller.apartment;


import com.zff.rental.AppConstant;
import com.zff.rental.common.PageVo;
import com.zff.rental.common.Result;
import com.zff.rental.service.AdminService;
import com.zff.rental.service.apartment.ApartmentPermissionServer;
import com.zff.rental.service.apartment.ApartmentServer;
import com.zff.rental.vo.apartment.apartment.*;
import com.zff.rental.vo.apartment.apartment.manager.AuditApartmentParams;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 公寓管理
 *
 * @author HCY
 */
@RestController
@RequestMapping(AppConstant.RM_APARTMENT_MANAGE)
public class ApartmentManageController {
    @Resource
    private ApartmentServer apartmentServer;
    @Resource
    private ApartmentPermissionServer apartmentPermissionServer;
    @Resource
    private AdminService adminService;

    /**
     * 添加公寓
     */
    @PostMapping("/add")
    public Result<ApartmentVo> add(@Validated @RequestBody AddApartmentParams params) {
        ApartmentVo result = apartmentServer.add(params);
        return Result.success(result);
    }

    /**
     * 修改公寓
     */
    @PostMapping("/modify")
    public Result<ApartmentVo> modify(@Validated @RequestBody ModifyApartmentParams params) {
        apartmentPermissionServer.checkPermission(params.getId());

        ApartmentVo result = apartmentServer.modify(params);
        return Result.success(result);
    }

    /**
     * 删除公寓
     */
    @GetMapping("/remove")
    public Result<Boolean> remove(@RequestParam("id") Long id) {
        apartmentPermissionServer.checkPermission(id);

        Boolean result = apartmentServer.remove(id);
        return Result.success(result);
    }

    /**
     * 公寓详情
     */
    @GetMapping("/details")
    public Result<ApartmentVo> details(@RequestParam("id") Long id) {
        ApartmentVo result = apartmentServer.details(id);
        return Result.success(result);
    }

    /**
     * 公寓列表
     */
    @PostMapping("/list")
    public Result<PageVo<ApartmentVo>> list(@RequestBody ApartmentListParams params) {
        PageVo<ApartmentVo> result = apartmentServer.list(params);
        return Result.success(result);
    }

    /**
     * 审核公寓
     */
    @PostMapping("/audit")
    public Result<Boolean> audit(@Validated @RequestBody AuditApartmentParams params) {
        if (!adminService.isAdmin()) {
            throw new RuntimeException("权限不足");
        }

        Boolean result = apartmentServer.audit(params);
        return Result.success(result);
    }

    /**
     * 简单公寓列表
     */
    @GetMapping("/simpleList")
    public Result<List<SimpleApartmentVo>> simpleList() {
        List<SimpleApartmentVo> result = apartmentServer.simpleList();
        return Result.success(result);
    }

    /**
     * 修改公寓排序
     */
    @PostMapping("/sort")
    public Result<Boolean> modifyApartmentSort(@RequestBody @Validated List<ModifyApartmentSortParams> params) {
        Boolean result = apartmentServer.modifyApartmentSort(params);
        return Result.success(result);
    }
}